/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.service.MuserService;
import com.cunyu.service.RoleGroupService;
import com.cunyu.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用户、角色、权限 管理
 * @author win
 * 2019/04/19-14:48
 */
@Controller
public class UserRoleController extends BaseController{

    @Resource
    RoleGroupService roleGroupService ;

    @Resource
    RoleService roleService ;

    @Resource
    MuserService muserService ;

    @RequestMapping("/users/page")
    public String page(Model model, ReqData page, Form form){
        RespData<List<Map<String, Object>>> users = muserService.getUsers(page,form);
        List<Map<String, Object>> roles = roleGroupService.getGroups();
        List<Map<String, Object>> pusers = muserService.getPusers();

        model.addAttribute("respData",users) ;
        model.addAttribute("roles",roles) ;
        model.addAttribute("pusers",pusers) ;
        model.addAttribute("form",form) ;
        return "users/page";
    }

    @RequestMapping("/users/upstate")
    public void upState( HttpServletResponse res, String id, String state){
        int i = muserService.upState(id,state) ;
        printOK(res,"success",i);
    }

    @RequestMapping("/users/user")
    public String user(Model model,String id){
        List<Map<String, Object>> roles = roleGroupService.getGroups();
        List<Map<String, Object>> pusers = muserService.getPusers();
        model.addAttribute("roles",roles) ;
        model.addAttribute("pusers",pusers) ;
        if(StringUtil.hasLength(id)){
            Map<String, Object> user = muserService.getUser(id);
            model.addAttribute("respData",user);
        }
        return "users/user" ;
    }

    @RequestMapping("/users/add")
    public void add(HttpServletResponse response, Form form) throws IOException {
        String id = form.getId();
        int change ;
        if(StringUtil.hasLength(id)){
            change = Integer.valueOf(id) ;
            muserService.updateUser(form) ;
        }else{
           change =  muserService.newUser(form) ;
        }
        response.sendRedirect("/users/page?change="+change);
    }

    @RequestMapping("/roles/list")
    public String roles(Model model,String change){
        List<Map<String, Object>> groups = roleGroupService.getGroups();
        model.addAttribute("respData",groups) ;
        model.addAttribute("change",change) ;
        return "roles/list" ;
    }

    @RequestMapping("/roles/tree")
    public String roleTree(Model model,String role){
        Integer oRole = null;
        if(StringUtil.hasLength(role)){
            oRole = Integer.valueOf(role) ;
        }
        Map<String, Map<String, Object>> roles = roleService.getRoles(oRole);
        model.addAttribute("respData",roles) ;
        return "roles/tree" ;
    }

    @RequestMapping("/roles/jsonTree")
    public void roleTreeJson(HttpServletResponse res,String role){
        Integer oRole = null;
        if(StringUtil.hasLength(role)){
            oRole = Integer.valueOf(role) ;
        }
        Map<String, Map<String, Object>> roles = roleService.getRoles(oRole);
        printOK(res,"success",roles);
    }

    @RequestMapping("/menu/menuInfo")
    public void menuInfoJson(HttpServletResponse res,String id){
        int iid = Integer.valueOf(id) ;
        Map<String, Object> roleinfo = roleService.getInfo(iid);
        printOK(res,"success",roleinfo);
    }

    /**
     * 获取所有的菜单列表
     */
    @RequestMapping("/menu/list")
    public String menuList(Model model,String change){
        Map<String, Map<String, Object>> roles = roleService.getRoles(null);
        model.addAttribute("respData",roles) ;
        model.addAttribute("change",change);
        return "menu/list" ;
    }

    /**
     * 修改 或 新增一个菜单的信息
     */
    @RequestMapping("/roles/roleData")
    public void menuListNU(Model model,Form form,HttpServletResponse response) throws IOException {
        int change ;
        if(StringUtil.hasLength(form.getId())){
            change = roleService.updateRole(form) ;
        }else {
            change = roleService.newRole(form) ;
        }
        response.sendRedirect("/menu/list?change="+change);
    }

    /**
     * 修改权限组的名称，并修改其对应的菜单信息
     */
    @RequestMapping("/roles/roleUpdate")
    public void roleUpdate(String role,String name,String[] id,HttpServletResponse response) throws IOException {
        int change ;
        if(StringUtil.hasLength(role)) {
            change = roleGroupService.updateGroup(role,id,name) ;
        }else {
            change = roleGroupService.newGroup(name,id) ;
        }
        response.sendRedirect("/roles/list?change="+change);
    }

    /**
     * 删除权限组，并消除其对应的菜单权限
     */
    @RequestMapping("/groups/del")
    public void delGroup(String role ,HttpServletResponse response) throws IOException {
        if(! "1".equals(role)){
            // 不要删除管理员权限
            roleGroupService.del(role) ;
        }
        response.sendRedirect("/roles/list");
    }

}
